import React, {PropTypes} from "react";
import {Router} from "dva/router";
// import pathToRegexp from 'path-to-regexp'
import Login from "./login";

const cached = {}
const registerModel = (app, model) => {
  if (!cached[model.namespace]) {
    app.model(model)
    cached[model.namespace] = 1
  }
}

const Routers = function ({ history, app }) {
  const handleChildRoute = ({ location, params, routes }) => {
    console.log(location, params, routes)
  }

  const routes = [
    {
      path: '/',
      component: Login,
      getIndexRoute (nextState, cb) {
       cb(null, Login)
      },
      childRoutes: [{
          path: 'app',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./app/AppModel'))
              cb(null, require('./app'))
            }, 'app')
          },
        },{
          path: 'dashboard',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./dashboard/DashboardModel'))
              cb(null, require('./dashboard'))
            }, 'dashboard')
          },
        }
      ],
    },
  ]

  routes[0].childRoutes.map(item => {
    item.onEnter = handleChildRoute
    return item
  })

  return <Router history={history} routes={routes} />
}

Routers.propTypes = {
  history: PropTypes.object,
  app: PropTypes.object,
}

export default Routers
